#!/bin/bash

if test -z "$arch"; then
	arch="i486"
	export arch
fi
VERSION=`date '+%Y%m%d'`
PKG=odhal
PARAMS='--owner 1000 --group 1000 --same-permissions --exclude "*~" --exclude .svn'

make || exit 1

rm -fr pkg/odhal*.tar.gz
chmod ug+x etc/init.d/odhal
chmod o-rwx etc/init.d/odhal
# Applying "admin" permission to configuration scripts
chmod 0770 usr/local/opendomo/services/config/*.sh
chmod 0770 usr/local/opendomo/services/syscript/configureControlPorts.sh
chmod 0770 usr/local/opendomo/services/syscript/add*.sh
chmod 0770 usr/local/opendomo/services/syscript/del*.sh
chmod 0770 usr/local/opendomo/services/syscript/edit*.sh

if ! test -d open-zwave; then
	svn checkout http://open-zwave.googlecode.com/svn/trunk/ open-zwave
fi

#rm -fr usr/share/ -fr 2>/dev/null
#mkdir -p usr/share/templates/arduino
#cp arduino/templates/* usr/share/templates/arduino/

strip usr/bin/*

echo "Dummy driver" > pkg/odhal_dummy.desc
tar cfz pkg/odhal_dummy-$VERSION.od.$arch.tar.gz usr/bin/dummy $PARAMS

echo "Micropik driver" > pkg/odhal_micropik.desc
tar cfz pkg/odhal_micropik-$VERSION.od.$arch.tar.gz usr/bin/micropik $PARAMS

echo "Arduino driver" > pkg/odhal_arduino.desc
tar cfz pkg/odhal_arduino-$VERSION.od.$arch.tar.gz usr/bin/arduino $PARAMS 

echo "X10 CM15a driver" > pkg/odhal_x10.desc
tar cfz pkg/odhal_x10-$VERSION.od.$arch.tar.gz var usr/bin/x10 var/pkg/odhal_x10_install.sh $PARAMS 

echo "ODControl driver" > pkg/odhal_domino.desc
tar cfz pkg/odhal_domino-$VERSION.od.$arch.tar.gz var usr/bin/domino $PARAMS 

if test -f usr/bin/zwave; then
	echo "Z-Wave driver" > pkg/odhal_zwave.desc
	tar cfz pkg/odhal_zwave-$VERSION.$arch.tar.gz usr/bin/zwave
fi

cp pkg/*.desc ./var/pkg
tar vcfz pkg/$PKG-$VERSION.od.noarch.tar.gz etc usr/local usr/share var $PARAMS 

